<?php

class m130111_093732_employee_work_report extends CDbMigration
{
	public function safeUp()
	{
		$this
		->createTable('erp_employee_daily_report',
			array(
				'id' => 'pk',
				'employeeId' => 'int',
				'currentProjectId' => 'int',
				'reportDate' => 'date',
				'workStatus' => 'int',
				'workAbsenceReason' => 'string',
				'workSummary' => 'string',
				'workDescription' => 'text',
				'workSuggestion' =>'text',
				'workTechnicProblem' =>'text',
				'workNextDayPlan' =>'text',
				'lateSubmit' => 'boolean not null',
				'createTime' => 'datetime',
				'createUserId' => 'int',
				'updateTime' => 'datetime',
				'updateUserId' => 'int',
			));
		$this
		->createIndex("employee_daily_report_create_user_id", 'erp_employee_daily_report', 'createUserId');
		$this
		->addForeignKey('fk_employee_daily_report_create_user_id', 'erp_employee_daily_report',
			'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
		->createIndex("employee_daily_report_update_user_id", 'erp_employee_daily_report', 'updateUserId');
		$this
		->addForeignKey('fk_employee_daily_report_update_user_id', 'erp_employee_daily_report',
			'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');

		$this
		->createIndex("employee_daily_report_employee_id", 'erp_employee_daily_report', 'employeeId');
		$this
		->addForeignKey('fk_employee_daily_report_employee_id', 'erp_employee_daily_report',
			'employeeId', 'erp_employee', 'id', 'SET NULL', 'CASCADE');

		$this->createIndex("employee_daily_report_project_id", 'erp_employee_daily_report', 'currentProjectId');
		$this
		->addForeignKey('fk_employee_daily_report_project_id', 'erp_employee_daily_report', 'currentProjectId',
			'erp_project', 'id', 'SET NULL', 'CASCADE');


		$this
		->createTable('erp_employee_weekly_report',
			array(
				'id' => 'pk',
				'employeeId' => 'int',
				'currentProjectId' => 'int',
				'reportWeekStartDate' => 'date',
				'workSummary' => 'string',
				'workLastWeekDescription' => 'text',
				'workNextWeekPlan' =>'text',
				'lateSubmit' => 'boolean not null',
				'createTime' => 'datetime',
				'createUserId' => 'int',
				'updateTime' => 'datetime',
				'updateUserId' => 'int',
			));
		$this
		->createIndex("employee_weekly_report_employee_id", 'erp_employee_weekly_report', 'employeeId');
		$this
		->addForeignKey('fk_employee_weekly_report_employee_id', 'erp_employee_weekly_report',
			'employeeId', 'erp_employee', 'id', 'SET NULL', 'CASCADE');

		$this->createIndex("employee_weekly_report_project_id", 'erp_employee_weekly_report', 'currentProjectId');
		$this
		->addForeignKey('fk_employee_weekly_report_project_id', 'erp_employee_weekly_report', 'currentProjectId',
			'erp_project', 'id', 'SET NULL', 'CASCADE');


		$this
		->createIndex("employee_weekly_report_create_user_id", 'erp_employee_weekly_report', 'createUserId');
		$this
		->addForeignKey('fk_employee_weekly_report_create_user_id', 'erp_employee_weekly_report',
			'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
		->createIndex("employee_weekly_report_update_user_id", 'erp_employee_weekly_report', 'updateUserId');
		$this
		->addForeignKey('fk_employee_weekly_report_update_user_id', 'erp_employee_weekly_report',
			'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');

		$this->addColumn('erp_user_access', 'employeeWorkReportViewProject', 'boolean');
		$this
		->insert('erp_auth_item',
			array(
				'name' => 'employeeWorkReportViewProject',
				'type' => CAuthItem::TYPE_ROLE,
				'bizrule' => 'return AccessControl::check("employeeWorkReportViewProject");',
			));
		$this
		->insert('erp_auth_item_child',
			array(
				'parent' => 'employee',
				'child' => 'employeeWorkReportViewProject',
			));

		$this->addColumn('erp_user_access', 'employeeWorkReportViewCompany', 'boolean');
		$this
		->insert('erp_auth_item',
			array(
				'name' => 'employeeWorkReportViewCompany',
				'type' => CAuthItem::TYPE_ROLE,
				'bizrule' => 'return AccessControl::check("employeeWorkReportViewCompany");',
			));
		$this
		->insert('erp_auth_item_child',
			array(
				'parent' => 'employee',
				'child' => 'employeeWorkReportViewCompany',
			));
	}

	public function safeDown()
	{
		$this
		->delete('erp_auth_item_child',
			array(
				'and',
				'parent=:parent',
				'child=:child',
			), array(
				':parent' => 'employee',
				':child' => 'employeeWorkReportViewProject',
			));
		$this
		->delete('erp_auth_item', 'name=:name', array(
			':name' => 'employeeWorkReportViewProject',
		));
		$this->dropColumn('erp_user_access', 'employeeWorkReportViewProject');

		$this
		->delete('erp_auth_item_child',
			array(
				'and',
				'parent=:parent',
				'child=:child',
			), array(
				':parent' => 'employee',
				':child' => 'employeeWorkReportViewCompany',
			));
		$this
		->delete('erp_auth_item', 'name=:name', array(
			':name' => 'employeeWorkReportViewCompany',
		));
		$this->dropColumn('erp_user_access', 'employeeWorkReportViewCompany');

		$this->dropTable('erp_employee_weekly_report');
		$this->dropTable('erp_employee_daily_report');
	}
}